package cn.yerly.sakura.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yerly.sakura.core.entity.ArcNotFoundException;
import cn.yerly.sakura.core.util.ApplicationUtils;
import cn.yerly.sakura.dao.model.User;
import cn.yerly.sakura.dao.repository.UserRepository;
import cn.yerly.sakura.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepository userRepository;

	@Override
	public User addModel(User t) {
		t.setId(ApplicationUtils.ID_GENERATOR.incrementAndGet());
		return userRepository.save(t);
	}

	@Override
	public User getModelById(Long id) {
		return userRepository.findOne(id);
	}

	@Override
	public User modifyModelById(User t) {
		User findOne = userRepository.findOne(t.getId());
		if (null == findOne) {
			throw new ArcNotFoundException("id为[" + t.getId() + "]的用户不存在");
		}
		findOne.setMobile(t.getMobile());
		return userRepository.save(findOne);
	}

	@Override
	public User removeModelById(Long id) {
		User findOne = userRepository.findOne(id);
		if (null == findOne) {
			throw new ArcNotFoundException("id为[" + id + "]的用户不存在");
		}
		findOne.setDisabled(true);
		return userRepository.save(findOne);
	}

	@Override
	public Iterable<User> getModels() {
		return userRepository.findAll();
	}

	
	
	

}
